package org.jeecg.modules.bus.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.annotation.ApiLogin;
import org.jeecg.com.util.ThirdSessionHolder;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.TenantContextHolder;
import org.jeecg.config.ApiVersion;
import org.jeecg.config.ApiVersionConstant;
import org.jeecg.config.WebConfig;
import org.jeecg.modules.business.dto.CouponsGenerateDto;
import org.jeecg.modules.business.dto.PostDataDto;
import org.jeecg.modules.business.dto.ProvideCouponsUsedDto;
import org.jeecg.modules.business.entity.BusMarketCoupons;
import org.jeecg.modules.business.entity.BusMarketCouponsCashUsed;
import org.jeecg.modules.business.entity.BusMarketCouponsUsed;
import org.jeecg.modules.business.enums.CouponsStatusEnum;
import org.jeecg.modules.business.service.IBusMarketCouponsService;
import org.jeecg.modules.business.service.IBusMarketCouponsUsedService;
import org.jeecg.modules.mall.entity.MallUserInfo;
import org.jeecg.modules.wxuser.service.IUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

@Api(tags="免房券")
@RestController
@AllArgsConstructor
@RequestMapping("/mall-api/coupons-used")
@Slf4j
public class CouponsUsedController extends WebConfig {
    @Resource
   private IBusMarketCouponsUsedService busMarketCouponsUsedService;
    @Resource
    private IBusMarketCouponsService busMarketCouponsService;
    @Resource
    private IUserInfoService userInfoService;
   /**
    * 获取我的可用优惠券分页列表查询
    *
    * @param busMarketCouponsUsed
    * @param pageNo
    * @param pageSize
    * @param req
    * @return
    */
   @ApiOperation(value="获取我的可用优惠券分页列表查询", notes="获取我的可用优惠券分页列表查询")
   @PostMapping(value = "/list")
   @ApiLogin
   @ApiVersion(group = ApiVersionConstant.FAP_MALLAPI101)
   public Result<IPage<BusMarketCouponsUsed>> queryPageList(BusMarketCouponsUsed busMarketCouponsUsed,
                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                  HttpServletRequest req) {
       String tenantId = TenantContextHolder.getTenantId();
       String userId = ThirdSessionHolder.getUserId();
       MallUserInfo mallUserInfo = userInfoService.getById(userId);
       if (StringUtils.isNotBlank(mallUserInfo.getMobile())) {
           LambdaQueryWrapper<BusMarketCouponsUsed> queryWrapper = QueryGenerator.initQueryWrapper(busMarketCouponsUsed, req.getParameterMap()).lambda();
           queryWrapper.eq(BusMarketCouponsUsed::getMobile, mallUserInfo.getMobile());
           queryWrapper.eq(BusMarketCouponsUsed::getTenantId, tenantId);
           queryWrapper.eq(BusMarketCouponsUsed::getStatus, 1);
           Page<BusMarketCouponsUsed> page = new Page<BusMarketCouponsUsed>(pageNo, pageSize);
           IPage<BusMarketCouponsUsed> pageList = busMarketCouponsUsedService.page(page, queryWrapper);
           if (pageList.getRecords().size() > 0) {
               pageList.getRecords().forEach(item -> {
                   BusMarketCoupons coupons = busMarketCouponsService.getById(item.getCouponsId());
                   if (coupons != null) {
                       item.setConponsName(coupons.getName());
                   }
                   CouponsStatusEnum couponsStatusEnum = CouponsStatusEnum.val(item.getStatus());
                   if (couponsStatusEnum != null) {
                       item.setStatusName(couponsStatusEnum.getTitle());
                   }
               });
           }
           return Result.OK(pageList);
       }
       return Result.OK(null);
   }


}
